﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.CodeIndex
{
    /// <summary>
    /// 编码索引管理类
    /// </summary>
    public class CateManager
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="parentCateGuid">父节点</param>
        /// <param name="cateName">名称</param>
        /// <param name="cateDesc">中文描述</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Add(string parentCateGuid, string cateName, string cateDesc, int seq)
        {
            try
            {
                string sql = string.Empty;
                string cateGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                sql = "Insert Into InfoCodeCates (CateGuid,ParentCateGuid,CateName,CateDesc,Seq) Values(@CateGuid,@ParentCateGuid,@CateName,@CateDesc,@Seq)";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@CateGuid", cateGuid),
                    SqlHelper.NewSqlParameter("@ParentCateGuid", parentCateGuid),
                    SqlHelper.NewSqlParameter("@CateName", cateName),
                    SqlHelper.NewSqlParameter("@CateDesc", cateDesc),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="cateGuid">索引Guid</param>
        /// <returns>是否成功</returns>
        public bool Delete(string cateGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Delete From InfoCodeCates Where CateGuid=@CateGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@CateGuid", cateGuid));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="cateGuid">索引Guid</param>
        /// <param name="cateName">索引名称</param>
        /// <param name="cateDesc">中文说明</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Update(string cateGuid, string cateName, string cateDesc, int seq)
        {
            try
            {
                string sql = string.Empty;
                sql = "Update InfoCodeCates Set CateName=@CateName,CateDesc=@CateDesc,Seq=@Seq Where CateGuid=@CateGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@CateGuid", cateGuid),
                    SqlHelper.NewSqlParameter("@CateName", cateName),
                    SqlHelper.NewSqlParameter("@CateDesc", cateDesc),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取索引列表（数据集）
        /// </summary>
        /// <param name="parentCateGuid">父节点</param>
        /// <returns></returns>
        public DataSet GetCateDs(string parentCateGuid)
        {
            try
            {
                string sql = "Select * From InfoCodeCates Where ParentCateGuid=@ParentCateGuid Order by Seq Asc";
                return SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@ParentCateGuid", parentCateGuid));
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取索引列表（数组）
        /// </summary>
        /// <param name="parentCateGuid">父节点</param>
        /// <returns></returns>
        public List<Cate> GetCateList(string parentCateGuid)
        {
            try
            {
                List<Cate> list = new List<Cate>();
                DataSet ds = this.GetCateDs(parentCateGuid);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        list.Add(new Cate(row["CateGuid"].ToString(), row["ParentCateGuid"].ToString(), row["CateName"].ToString(), row["CateDesc"].ToString(), int.Parse(row["Seq"].ToString())));
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 判断是否有子索引
        /// </summary>
        /// <param name="cateGuid">索引Guid</param>
        /// <returns></returns>
        public bool ExistChild(string cateGuid)
        {
            try
            {
                string sql = "Select CateGuid From InfoCodeCates Where ParentCateGuid=@ParentCateGuid";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@ParentCateGuid", cateGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取多级索引的XML文本
        /// </summary>
        /// <returns></returns>
        public XmlDocument GetCateXmlDoc()
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelement;

            xmldoc = new XmlDocument();
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            xmlelement = xmldoc.CreateElement("root");
            xmldoc.AppendChild(xmlelement);

            this.RecursionCate(ref xmldoc, ref xmlelement, "0");

            return xmldoc;
        }

        /// <summary>
        /// 递归多级索引
        /// </summary>
        /// <param name="xmldoc">XML文本</param>
        /// <param name="parentXmlElement">XML父节点</param>
        /// <param name="parentCateGuid">父节点</param>
        private void RecursionCate(ref XmlDocument xmldoc, ref XmlElement parentXmlElement, string parentCateGuid)
        {
            XmlElement xmlelement;
            List<Cate> list = this.GetCateList(parentCateGuid);
            foreach (Cate cate in list)
            {
                xmlelement = xmldoc.CreateElement("root_" + cate.CateGuid);
                xmlelement.SetAttribute("id", cate.CateGuid);
                xmlelement.SetAttribute("desc", cate.CateName);
                parentXmlElement.AppendChild(xmlelement);

                this.RecursionCate(ref xmldoc, ref xmlelement, cate.CateGuid);
            }
        }
    }
}

